package cn.wolfcode.config;

import cn.wolfcode.job.FileCustomElasticJob;
import cn.wolfcode.job.FileDataflowJob;
import cn.wolfcode.job.MyElasticJob;

import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

/**
 *  任务调度 配置类
 */
@Configuration
public class ElasticJobConfig {
    @Autowired
    private MyElasticJob myElasticJob;
    @Autowired
    private CoordinatorRegistryCenter registryCenter;
    
    // 任务分配的信息
    private static LiteJobConfiguration createJobConfiguration(final Class<? extends ElasticJob> jobClass,
                                                               final String cron, // cron表达式
                                                               final int shardingTotalCount,// 分片总数
                                                               final String shardingItemParameters,
                                                               boolean dataflowType) {
        // 定义作业核心配置
        JobCoreConfiguration.Builder jobCoreConfigurationBuilder = JobCoreConfiguration.newBuilder(jobClass.getSimpleName(), cron, shardingTotalCount);
        if(!StringUtils.isEmpty(shardingItemParameters)){
            jobCoreConfigurationBuilder.shardingItemParameters(shardingItemParameters);
        }

        // 定义类型配置 分2种（简单类型 Simple   和  Dataflow类型 ）
        // 判断时那种类型的bean
        JobTypeConfiguration jobConfig = null;
        if(dataflowType){
            // Dataflow类型调度任务
            jobConfig = new DataflowJobConfiguration(jobCoreConfigurationBuilder.build(),jobClass.getCanonicalName(),true);

        }else{
            // 定义SIMPLE类型配置
            jobConfig = new SimpleJobConfiguration(jobCoreConfigurationBuilder.build(), jobClass.getCanonicalName());
        }

        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(jobConfig).overwrite(true).build();
        return simpleJobRootConfig;
    }
    
    // 把 SpringJobScheduler 交给spring管理，在SpringJobScheduler中添加任务类（这个是 myElasticJob ）
/*    @Bean(initMethod = "init")
    public SpringJobScheduler initSimpleElasticJob(){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(myElasticJob,registryCenter,createJobConfiguration(MyElasticJob.class,"0/3 * * * * ?",1,null, false));
        return springJobScheduler;
    }*/

    // 任务类 交给spring  非分片式
/*    @Bean(initMethod = "init")
    public SpringJobScheduler initFileCustomElasticJob(FileCustomElasticJob fileCustomElasticJob){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(fileCustomElasticJob,registryCenter,createJobConfiguration(FileCustomElasticJob.class,"0/10 * * * * ?",1,null));
        return springJobScheduler;
    }*/

    // 分片方式
    @Bean(initMethod = "init")
    public SpringJobScheduler initFileCustomElasticJob(FileCustomElasticJob fileCustomElasticJob){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(
                fileCustomElasticJob,
                registryCenter,
                createJobConfiguration(FileCustomElasticJob.class,"0/10 * * * * ?",4,"0=text,1=image,2=radio,3=vedio", false));// 4表示分为四片，后面是分片参数
        return springJobScheduler;
    }

    // Dataflow类型调度任务
 /*   @Bean(initMethod = "init")
    public SpringJobScheduler iniDataflowElasticJob(FileDataflowJob fileDataflowJob){
        SpringJobScheduler springJobScheduler = new SpringJobScheduler(
                fileDataflowJob,
                registryCenter,
                createJobConfiguration(FileDataflowJob.class,"0/3 * * * * ?",1,null,true));
        return springJobScheduler;
    }*/
}